package drh.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.ScalarHandler;

import drh.bean.OrderDetail;
import drh.dao.BaseDao;
import drh.dao.OrderDetailDao;
import handler.OrderDetailResultSetHandler;

public class OrderDetailDaoImpl extends BaseDao implements OrderDetailDao {

	@Override
	public Integer deleteOrderDetailById(Integer id) {
		try {
			return query.update("delete from orderdetail where id=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public Integer updateOrderDetail(OrderDetail info) {
		try {
			return query.update("update orderdetail set orderid=?,amount=?,money=?,productid=?,time=?,isdelete=?,userid=? where id=?",info.getOrderid(),info.getAmount(),info.getMoney(),
			info.getProductid(),info.getTime(),info.getIsdelete(),info.getUserid(),info.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public Integer addOrderDetail(OrderDetail info) {
		try {
			return query.update("insert into orderdetail(orderid,amount,money,productid,time,isdelete,userid) values(?,?,?,?,?,?,?)",info.getOrderid(),info.getAmount(),info.getMoney(),
					info.getProductid(),info.getTime(),info.getIsdelete(),info.getUserid());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<OrderDetail> GetOrderDetailPaging(Integer pageIndex, Integer pageSize) {
		String sql = "select * from orderdetail limit ?,?";
		try {
			return query.query(sql, new OrderDetailResultSetHandler(),(pageIndex-1)*pageSize,pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	return null;
	}

	@Override
	public Integer getCount() {
		String sql = "select count(1) from orderdetail";
		Object query2;
		try {
			query2 = query.query(sql, new ScalarHandler());
			return Integer.parseInt(query2.toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}	
		return null;
	}
	
}
